<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>自制深度学习框架--实现Yolov5的推理 | kiloGrand</title><meta name="keywords" content="kuiper_infer"><meta name="author" content="kiloGrand"><meta name="copyright" content="kiloGrand"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="为了实现Yolov5的推理，我们需要在source&#x2F;layer&#x2F;details中实现SiLU、Concat、UpSample和YoloDetect。详细请看：https:&#x2F;&#x2F;blog.csdn.net&#x2F;qq_32901731&#x2F;article&#x2F;details&#x2F;129710271 SiLUSiLU(x)&#x3D;\frac{x}{1+e^{-x}}相当于sigmoid函数的乘以x。 1234567891011">
<meta property="og:type" content="article">
<meta property="og:title" content="自制深度学习框架--实现Yolov5的推理">
<meta property="og:url" content="https://kilogrand.gitee.io/2023/03/24/kuiper_infer-L14/index.html">
<meta property="og:site_name" content="kiloGrand">
<meta property="og:description" content="为了实现Yolov5的推理，我们需要在source&#x2F;layer&#x2F;details中实现SiLU、Concat、UpSample和YoloDetect。详细请看：https:&#x2F;&#x2F;blog.csdn.net&#x2F;qq_32901731&#x2F;article&#x2F;details&#x2F;129710271 SiLUSiLU(x)&#x3D;\frac{x}{1+e^{-x}}相当于sigmoid函数的乘以x。 1234567891011">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://kilogrand.gitee.io/img/coding.jpg">
<meta property="article:published_time" content="2023-03-24T12:00:00.000Z">
<meta property="article:modified_time" content="2023-04-22T03:09:06.854Z">
<meta property="article:author" content="kiloGrand">
<meta property="article:tag" content="kuiper_infer">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://kilogrand.gitee.io/img/coding.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://kilogrand.gitee.io/2023/03/24/kuiper_infer-L14/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '自制深度学习框架--实现Yolov5的推理',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-04-22 11:09:06'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/profile.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">46</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/coding.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">kiloGrand</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">自制深度学习框架--实现Yolov5的推理</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-03-24T12:00:00.000Z" title="发表于 2023-03-24 20:00:00">2023-03-24</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-04-22T03:09:06.854Z" title="更新于 2023-04-22 11:09:06">2023-04-22</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">深度学习</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>13分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="自制深度学习框架--实现Yolov5的推理"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>为了实现Yolov5的推理，我们需要在<code>source/layer/details</code>中实现SiLU、Concat、UpSample和YoloDetect。<br>详细请看：<a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_32901731/article/details/129710271">https://blog.csdn.net/qq_32901731/article/details/129710271</a></p>
<h2 id="SiLU"><a href="#SiLU" class="headerlink" title="SiLU"></a>SiLU</h2><script type="math/tex; mode=display">SiLU(x)=\frac{x}{1+e^{-x}}</script><p>相当于sigmoid函数的乘以x。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">InferStatus <span class="title">SiLULayer::Forward</span><span class="params">(<span class="type">const</span> std::vector&lt;std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;&gt; &amp;inputs,</span></span></span><br><span class="line"><span class="params"><span class="function">                               std::vector&lt;std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;&gt; &amp;outputs)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (inputs.<span class="built_in">empty</span>()) &#123;</span><br><span class="line">    <span class="built_in">LOG</span>(ERROR) &lt;&lt; <span class="string">&quot;The input feature map of silu layer is empty&quot;</span>;</span><br><span class="line">    <span class="keyword">return</span> InferStatus::kInferFailedInputEmpty;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (inputs.<span class="built_in">size</span>() != outputs.<span class="built_in">size</span>()) &#123;</span><br><span class="line">    <span class="built_in">LOG</span>(ERROR) &lt;&lt; <span class="string">&quot;The input and output size of silu layer is not adapting&quot;</span>;</span><br><span class="line">    <span class="keyword">return</span> InferStatus::kInferFailedInputOutSizeAdaptingError;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="type">const</span> <span class="type">uint32_t</span> batch_size = inputs.<span class="built_in">size</span>();</span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> omp parallel for num_threads(batch_size)</span></span><br><span class="line">  <span class="keyword">for</span> (<span class="type">uint32_t</span> i = <span class="number">0</span>; i &lt; batch_size; ++i) &#123;</span><br><span class="line">    <span class="comment">// 遍历每一批次</span></span><br><span class="line">    <span class="type">const</span> std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt; &amp;input = inputs.<span class="built_in">at</span>(i);</span><br><span class="line">    <span class="built_in">CHECK</span>(input == <span class="literal">nullptr</span> || !input-&gt;<span class="built_in">empty</span>()) &lt;&lt; <span class="string">&quot;The input feature map of silu layer is empty!&quot;</span>;</span><br><span class="line"></span><br><span class="line">    std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt; output = outputs.<span class="built_in">at</span>(i);</span><br><span class="line">    <span class="keyword">if</span> (output == <span class="literal">nullptr</span> || output-&gt;<span class="built_in">empty</span>()) &#123;</span><br><span class="line">      output = std::make_shared&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;(input-&gt;<span class="built_in">shapes</span>());</span><br><span class="line">      outputs.<span class="built_in">at</span>(i) = output;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">CHECK</span>(output-&gt;<span class="built_in">shapes</span>() == input-&gt;<span class="built_in">shapes</span>()) &lt;&lt; <span class="string">&quot;The output size of silu layer is error&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 将input中的数据拷贝到output</span></span><br><span class="line">    output-&gt;<span class="built_in">set_data</span>(input-&gt;<span class="built_in">data</span>());</span><br><span class="line">    <span class="comment">// SiLU</span></span><br><span class="line">    output-&gt;<span class="built_in">Transform</span>([](<span class="type">const</span> <span class="type">float</span> value) &#123;</span><br><span class="line">      <span class="keyword">return</span> value / (<span class="number">1.f</span> + <span class="built_in">expf</span>(-value));</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> InferStatus::kInferSuccess;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="Concat"><a href="#Concat" class="headerlink" title="Concat"></a>Concat</h2><p>将多个张量在通道维(channel dim)进行拼接<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">InferStatus <span class="title">CatLayer::Forward</span><span class="params">(</span></span></span><br><span class="line"><span class="params"><span class="function">    <span class="type">const</span> std::vector&lt;std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;&gt;&amp; inputs,</span></span></span><br><span class="line"><span class="params"><span class="function">    std::vector&lt;std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;&gt;&amp; outputs)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (inputs.<span class="built_in">empty</span>()) &#123;</span><br><span class="line">    <span class="built_in">LOG</span>(ERROR) &lt;&lt; <span class="string">&quot;The input feature map of cat layer is empty&quot;</span>;</span><br><span class="line">    <span class="keyword">return</span> InferStatus::kInferFailedInputEmpty;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (inputs.<span class="built_in">size</span>() == outputs.<span class="built_in">size</span>()) &#123;</span><br><span class="line">    <span class="built_in">LOG</span>(ERROR) &lt;&lt; <span class="string">&quot;The input and output size is not adapting&quot;</span>;</span><br><span class="line">    <span class="keyword">return</span> InferStatus::kInferFailedInputOutSizeAdaptingError;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (dim_ != <span class="number">1</span> &amp;&amp; dim_ != <span class="number">-3</span>) &#123;</span><br><span class="line">    <span class="built_in">LOG</span>(ERROR) &lt;&lt; <span class="string">&quot;The dimension of cat layer is error&quot;</span>;</span><br><span class="line">    <span class="keyword">return</span> InferStatus::kInferFailedDimensionParameterError;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="type">const</span> <span class="type">uint32_t</span> output_size = outputs.<span class="built_in">size</span>();</span><br><span class="line">  <span class="built_in">CHECK</span>(inputs.<span class="built_in">size</span>() % output_size == <span class="number">0</span>);</span><br><span class="line">  <span class="type">const</span> <span class="type">uint32_t</span> packet_size = inputs.<span class="built_in">size</span>() / output_size;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> omp parallel for num_threads(outputs.size())</span></span><br><span class="line">  <span class="keyword">for</span> (<span class="type">uint32_t</span> i = <span class="number">0</span>; i &lt; outputs.<span class="built_in">size</span>(); ++i) &#123;</span><br><span class="line">    std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt; output = outputs.<span class="built_in">at</span>(i);</span><br><span class="line">    <span class="type">uint32_t</span> start_channel = <span class="number">0</span>;</span><br><span class="line">    <span class="type">uint32_t</span> rows = inputs.<span class="built_in">front</span>()-&gt;<span class="built_in">rows</span>();</span><br><span class="line">    <span class="type">uint32_t</span> cols = inputs.<span class="built_in">front</span>()-&gt;<span class="built_in">cols</span>();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">uint32_t</span> j = i; j &lt; inputs.<span class="built_in">size</span>(); j += output_size) &#123;</span><br><span class="line">      <span class="type">const</span> std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;&amp; input = inputs.<span class="built_in">at</span>(j);</span><br><span class="line">      <span class="built_in">CHECK</span>(input != <span class="literal">nullptr</span> &amp;&amp; !input-&gt;<span class="built_in">empty</span>())</span><br><span class="line">          &lt;&lt; <span class="string">&quot;The input feature map of cat layer is empty&quot;</span>;</span><br><span class="line"></span><br><span class="line">      <span class="type">const</span> <span class="type">uint32_t</span> in_channels = input-&gt;<span class="built_in">channels</span>();</span><br><span class="line">      <span class="built_in">CHECK</span>(rows == input-&gt;<span class="built_in">rows</span>() &amp;&amp; cols == input-&gt;<span class="built_in">cols</span>());</span><br><span class="line"></span><br><span class="line">      <span class="keyword">if</span> (output == <span class="literal">nullptr</span> || output-&gt;<span class="built_in">empty</span>()) &#123;</span><br><span class="line">        output = std::make_shared&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;(in_channels * packet_size,</span><br><span class="line">                                                 rows, cols);</span><br><span class="line">        outputs.<span class="built_in">at</span>(i) = output;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      <span class="comment">// 检查output的通道数量等于input数组的数量乘以input的维度</span></span><br><span class="line">      <span class="built_in">CHECK</span>(output-&gt;<span class="built_in">channels</span>() == in_channels * packet_size &amp;&amp;</span><br><span class="line">            output-&gt;<span class="built_in">rows</span>() == rows &amp;&amp; output-&gt;<span class="built_in">cols</span>() == cols);</span><br><span class="line"></span><br><span class="line">      <span class="comment">// 将逐个输入在output的通道维上拼接起来</span></span><br><span class="line">      <span class="keyword">for</span> (<span class="type">uint32_t</span> c = <span class="number">0</span>; c &lt; in_channels; ++c) &#123;</span><br><span class="line">        output-&gt;<span class="built_in">slice</span>(start_channel + c) = input-&gt;<span class="built_in">slice</span>(c);</span><br><span class="line">      &#125;</span><br><span class="line">      start_channel += input-&gt;<span class="built_in">channels</span>();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> InferStatus::kInferSuccess;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="UpSample"><a href="#UpSample" class="headerlink" title="UpSample"></a>UpSample</h2><p>输入的大小(width和height)放大到指定的scale倍而已，放大的方法这里采用了nearest方法，<br>也就是通过复制最近点的值来进行上采样。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">InferStatus <span class="title">UpSampleLayer::Forward</span><span class="params">(<span class="type">const</span> std::vector&lt;std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;&gt; &amp;inputs,</span></span></span><br><span class="line"><span class="params"><span class="function">                                   std::vector&lt;std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;&gt; &amp;outputs)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (inputs.<span class="built_in">empty</span>()) &#123;</span><br><span class="line">    <span class="built_in">LOG</span>(ERROR) &lt;&lt; <span class="string">&quot;The input feature map of upsample layer is empty&quot;</span>;</span><br><span class="line">    <span class="keyword">return</span> InferStatus::kInferFailedInputEmpty;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (inputs.<span class="built_in">size</span>() != outputs.<span class="built_in">size</span>()) &#123;</span><br><span class="line">    <span class="built_in">LOG</span>(ERROR) &lt;&lt; <span class="string">&quot;The input and output size is not adapting&quot;</span>;</span><br><span class="line">    <span class="keyword">return</span> InferStatus::kInferFailedInputOutSizeAdaptingError;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span> (<span class="type">uint32_t</span> i = <span class="number">0</span>; i &lt; inputs.<span class="built_in">size</span>(); ++i) &#123;</span><br><span class="line">    <span class="type">const</span> sftensor &amp;input_data = inputs.<span class="built_in">at</span>(i);</span><br><span class="line">    <span class="keyword">if</span> (input_data == <span class="literal">nullptr</span> || input_data-&gt;<span class="built_in">empty</span>()) &#123;</span><br><span class="line">      <span class="built_in">LOG</span>(ERROR) &lt;&lt; <span class="string">&quot;The input feature map of upsample layer is empty&quot;</span>;</span><br><span class="line">      <span class="keyword">return</span> InferStatus::kInferFailedInputEmpty;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="built_in">LOG_IF</span>(FATAL, <span class="keyword">this</span>-&gt;mode_ != UpSampleMode::kModeNearest) &lt;&lt; <span class="string">&quot;Unsupported upsample mode: &quot;</span> &lt;&lt; <span class="built_in">int</span>(mode_);</span><br><span class="line"></span><br><span class="line">  <span class="type">const</span> <span class="type">uint32_t</span> batch_size = inputs.<span class="built_in">size</span>();</span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> omp parallel for num_threads(batch_size)</span></span><br><span class="line">  <span class="keyword">for</span> (<span class="type">uint32_t</span> i = <span class="number">0</span>; i &lt; batch_size; ++i) &#123;</span><br><span class="line">    <span class="comment">// 遍历每一批次</span></span><br><span class="line">    <span class="type">const</span> arma::fcube &amp;input_data = inputs.<span class="built_in">at</span>(i)-&gt;<span class="built_in">data</span>();</span><br><span class="line"></span><br><span class="line">    std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt; output = outputs.<span class="built_in">at</span>(i);</span><br><span class="line">    <span class="keyword">if</span> (output == <span class="literal">nullptr</span> || output-&gt;<span class="built_in">empty</span>()) &#123;</span><br><span class="line">      output = std::make_shared&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;(input_data.n_slices,</span><br><span class="line">                                               <span class="built_in">uint32_t</span>(input_data.n_rows * scale_h_),</span><br><span class="line">                                               <span class="built_in">uint32_t</span>(input_data.n_cols * scale_w_));</span><br><span class="line">      outputs.<span class="built_in">at</span>(i) = output;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">auto</span> &amp;output_data = output-&gt;<span class="built_in">data</span>();</span><br><span class="line">    <span class="comment">// 检查output的空间是否放得下上采样后的输入</span></span><br><span class="line">    <span class="built_in">CHECK</span>(output_data.n_rows == input_data.n_rows * scale_h_) &lt;&lt; <span class="string">&quot;The height of the feature map is not adapting!&quot;</span>;</span><br><span class="line">    <span class="built_in">CHECK</span>(output_data.n_cols == input_data.n_cols * scale_w_) &lt;&lt; <span class="string">&quot;The width of the feature map is not adapting!&quot;</span>;</span><br><span class="line">    <span class="built_in">CHECK</span>(input_data.n_slices == output_data.n_slices) &lt;&lt; <span class="string">&quot;The channel of the feature map is not adapting!&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="type">const</span> <span class="type">uint32_t</span> channels = input_data.n_slices;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">uint32_t</span> c = <span class="number">0</span>; c &lt; channels; ++c) &#123;</span><br><span class="line">      <span class="comment">// 遍历每一通道</span></span><br><span class="line">      <span class="type">const</span> arma::fmat &amp;input_channel = input_data.<span class="built_in">slice</span>(c);</span><br><span class="line">      arma::fmat &amp;output_channel = output_data.<span class="built_in">slice</span>(c);</span><br><span class="line">      <span class="type">const</span> <span class="type">uint32_t</span> output_w = output_channel.n_cols;</span><br><span class="line">      <span class="type">const</span> <span class="type">uint32_t</span> output_h = output_channel.n_rows;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">for</span> (<span class="type">uint32_t</span> w = <span class="number">0</span>; w &lt; output_w; ++w) &#123;</span><br><span class="line">        <span class="comment">// 将output_channel上的坐标除以scale_w，得到它在输入input_channel上的坐标src_w</span></span><br><span class="line">        <span class="type">const</span> <span class="type">uint32_t</span> src_w = <span class="built_in">uint32_t</span>((<span class="type">float</span>) w / <span class="keyword">this</span>-&gt;scale_w_);</span><br><span class="line">        <span class="built_in">CHECK</span>(src_w &lt; input_channel.n_cols);</span><br><span class="line"></span><br><span class="line">        <span class="type">float</span> *output_channel_ptr = output_channel.<span class="built_in">colptr</span>(w);</span><br><span class="line">        <span class="type">const</span> <span class="type">float</span> *input_channel_ptr = input_channel.<span class="built_in">colptr</span>(src_w);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">uint32_t</span> h = <span class="number">0</span>; h &lt; output_h; ++h) &#123;</span><br><span class="line">          <span class="comment">// 将output_channel上的坐标除以scale_h，得到它在输入input_channel上的坐标src_h</span></span><br><span class="line">          <span class="type">const</span> <span class="type">uint32_t</span> src_h = <span class="built_in">uint32_t</span>((<span class="type">float</span>) h / <span class="keyword">this</span>-&gt;scale_h_);</span><br><span class="line">          <span class="built_in">CHECK</span>(src_h &lt; input_channel.n_rows);</span><br><span class="line"></span><br><span class="line">          <span class="comment">// 根据src_h和src_w位置的值来赋值</span></span><br><span class="line">          <span class="type">const</span> <span class="type">float</span> src_value = *(input_channel_ptr + src_h);</span><br><span class="line">          *(output_channel_ptr + h) = src_value;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> InferStatus::kInferSuccess;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="YoloDetect"><a href="#YoloDetect" class="headerlink" title="YoloDetect"></a>YoloDetect</h2><p>YoloDetect的Python定义如下，直接摘录自YoloV5项目的yolo.py文件。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">forward</span>(<span class="params">self, x</span>):</span><br><span class="line">    z = []  <span class="comment"># inference output</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(self.nl):</span><br><span class="line">        x[i] = self.m[i](x[i])  <span class="comment"># conv</span></span><br><span class="line">        bs, _, ny, nx = x[i].shape  <span class="comment"># x(bs,255,20,20) to x(bs,3,20,20,85)</span></span><br><span class="line">        x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(<span class="number">0</span>, <span class="number">1</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">2</span>).contiguous()</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> self.training:  <span class="comment"># inference</span></span><br><span class="line">            省略...</span><br><span class="line">            <span class="keyword">else</span>:  <span class="comment"># Detect (boxes only)</span></span><br><span class="line">                xy, wh, conf = x[i].sigmoid().split((<span class="number">2</span>, <span class="number">2</span>, self.nc + <span class="number">1</span>), <span class="number">4</span>)</span><br><span class="line">                xy = (xy * <span class="number">2</span> + self.grid[i]) * self.stride[i]  <span class="comment"># xy</span></span><br><span class="line">                wh = (wh * <span class="number">2</span>) ** <span class="number">2</span> * self.anchor_grid[i]  <span class="comment"># wh</span></span><br><span class="line">                y = torch.cat((xy, wh, conf), <span class="number">4</span>)</span><br><span class="line">            z.append(y.view(bs, self.na * nx * ny, self.no))</span><br></pre></td></tr></table></figure></p>
<p>YoloDetectLayer的定义<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">YoloDetectLayer</span> : <span class="keyword">public</span> Layer &#123;</span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line">  <span class="function"><span class="keyword">explicit</span> <span class="title">YoloDetectLayer</span><span class="params">(<span class="type">int32_t</span> stages,</span></span></span><br><span class="line"><span class="params"><span class="function">                           <span class="type">int32_t</span> num_classes,</span></span></span><br><span class="line"><span class="params"><span class="function">                           <span class="type">const</span> std::vector&lt;<span class="type">float</span>&gt; &amp;strides,</span></span></span><br><span class="line"><span class="params"><span class="function">                           <span class="type">const</span> std::vector&lt;arma::fmat&gt; &amp;anchor_grids,</span></span></span><br><span class="line"><span class="params"><span class="function">                           <span class="type">const</span> std::vector&lt;arma::fmat&gt; &amp;grids,</span></span></span><br><span class="line"><span class="params"><span class="function">                           <span class="type">const</span> std::vector&lt;std::shared_ptr&lt;ConvolutionLayer&gt;&gt; &amp;conv_layers)</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function">InferStatus <span class="title">Forward</span><span class="params">(<span class="type">const</span> std::vector&lt;std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;&gt; &amp;inputs,</span></span></span><br><span class="line"><span class="params"><span class="function">                      std::vector&lt;std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;&gt; &amp;outputs)</span> <span class="keyword">override</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">static</span> ParseParameterAttrStatus <span class="title">GetInstance</span><span class="params">(<span class="type">const</span> std::shared_ptr&lt;RuntimeOperator&gt; &amp;op,</span></span></span><br><span class="line"><span class="params"><span class="function">                                              std::shared_ptr&lt;Layer&gt; &amp;yolo_detect_layer)</span></span>;</span><br><span class="line"> <span class="keyword">private</span>:</span><br><span class="line">  <span class="type">int32_t</span> stages_ = <span class="number">0</span>;</span><br><span class="line">  <span class="type">int32_t</span> num_classes_ = <span class="number">0</span>;</span><br><span class="line">  std::vector&lt;<span class="type">float</span>&gt; strides_;</span><br><span class="line">  std::vector&lt;arma::fmat&gt; anchor_grids_;</span><br><span class="line">  std::vector&lt;arma::fmat&gt; grids_;</span><br><span class="line">  std::vector&lt;std::shared_ptr&lt;ConvolutionLayer&gt;&gt; conv_layers_;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<p>YoloDetectLayer::Forward实现<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">InferStatus <span class="title">YoloDetectLayer::Forward</span><span class="params">(</span></span></span><br><span class="line"><span class="params"><span class="function">    <span class="type">const</span> std::vector&lt;std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;&gt; &amp;inputs,</span></span></span><br><span class="line"><span class="params"><span class="function">    std::vector&lt;std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;&gt; &amp;outputs)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (inputs.<span class="built_in">empty</span>()) &#123;</span><br><span class="line">    <span class="built_in">LOG</span>(ERROR) &lt;&lt; <span class="string">&quot;The input feature map of yolo detect layer is empty&quot;</span>;</span><br><span class="line">    <span class="keyword">return</span> InferStatus::kInferFailedInputEmpty;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="type">const</span> <span class="type">uint32_t</span> stages = stages_;</span><br><span class="line">  <span class="type">const</span> <span class="type">uint32_t</span> classes_info = num_classes_ + <span class="number">5</span>;</span><br><span class="line">  <span class="type">const</span> <span class="type">uint32_t</span> input_size = inputs.<span class="built_in">size</span>();</span><br><span class="line">  <span class="type">const</span> <span class="type">uint32_t</span> batch_size = outputs.<span class="built_in">size</span>();</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (input_size / batch_size != stages_ || input_size % batch_size != <span class="number">0</span>) &#123;</span><br><span class="line">    <span class="built_in">LOG</span>(ERROR) &lt;&lt; <span class="string">&quot;The input and output number of yolo detect layer is wrong&quot;</span>;</span><br><span class="line">    <span class="keyword">return</span> InferStatus::kInferFailedYoloStageNumberError;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="built_in">CHECK</span>(!<span class="keyword">this</span>-&gt;conv_layers_.<span class="built_in">empty</span>() &amp;&amp; <span class="keyword">this</span>-&gt;conv_layers_.<span class="built_in">size</span>() == stages)</span><br><span class="line">          &lt;&lt; <span class="string">&quot;The convolution layers in yolo detection layer is empty or do not &quot;</span></span><br><span class="line">             <span class="string">&quot;have a correct number&quot;</span>;</span><br><span class="line"></span><br><span class="line">  std::vector&lt;std::vector&lt;std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;&gt;&gt; <span class="built_in">batches</span>(stages);</span><br><span class="line">  <span class="keyword">for</span> (<span class="type">uint32_t</span> i = <span class="number">0</span>; i &lt; input_size; ++i) &#123;</span><br><span class="line">    <span class="type">const</span> <span class="type">uint32_t</span> index = i / batch_size;</span><br><span class="line">    <span class="type">const</span> <span class="keyword">auto</span> &amp;input_data = inputs.<span class="built_in">at</span>(i);</span><br><span class="line">    <span class="keyword">if</span> (input_data == <span class="literal">nullptr</span> || input_data-&gt;<span class="built_in">empty</span>()) &#123;</span><br><span class="line">      <span class="built_in">LOG</span>(ERROR) &lt;&lt; <span class="string">&quot;The input feature map of yolo detect layer is empty&quot;</span>;</span><br><span class="line">      <span class="keyword">return</span> InferStatus::kInferFailedInputEmpty;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">CHECK</span>(index &lt;= batches.<span class="built_in">size</span>());</span><br><span class="line">    batches.<span class="built_in">at</span>(index).<span class="built_in">push_back</span>(input_data);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// conv</span></span><br><span class="line">  std::vector&lt;std::vector&lt;sftensor&gt;&gt; <span class="built_in">stage_outputs</span>(stages);</span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> omp parallel for num_threads(stages)</span></span><br><span class="line">  <span class="keyword">for</span> (<span class="type">uint32_t</span> stage = <span class="number">0</span>; stage &lt; stages; ++stage) &#123;</span><br><span class="line">    <span class="type">const</span> std::vector&lt;std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;&gt; &amp;stage_input =</span><br><span class="line">        batches.<span class="built_in">at</span>(stage);</span><br><span class="line"></span><br><span class="line">    <span class="built_in">CHECK</span>(stage_input.<span class="built_in">size</span>() == batch_size)</span><br><span class="line">            &lt;&lt; <span class="string">&quot;The number of stage do not equal to batch size&quot;</span>;</span><br><span class="line"></span><br><span class="line">    std::vector&lt;std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;&gt; <span class="built_in">stage_output</span>(batch_size);</span><br><span class="line">    <span class="type">const</span> <span class="keyword">auto</span> status =</span><br><span class="line">        <span class="keyword">this</span>-&gt;conv_layers_.<span class="built_in">at</span>(stage)-&gt;<span class="built_in">Forward</span>(stage_input, stage_output);</span><br><span class="line"></span><br><span class="line">    <span class="built_in">CHECK</span>(status == InferStatus::kInferSuccess)</span><br><span class="line">            &lt;&lt; <span class="string">&quot;Infer failed, error code: &quot;</span> &lt;&lt; <span class="built_in">int</span>(status);</span><br><span class="line">    <span class="built_in">CHECK</span>(stage_output.<span class="built_in">size</span>() == batch_size)</span><br><span class="line">            &lt;&lt; <span class="string">&quot;The number of stage output do not equal to batch size&quot;</span>;</span><br><span class="line">    stage_outputs.<span class="built_in">at</span>(stage) = stage_output;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="type">uint32_t</span> concat_rows = <span class="number">0</span>;</span><br><span class="line">  std::vector&lt;std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;&gt; <span class="built_in">zs</span>(stages);</span><br><span class="line">  <span class="keyword">for</span> (<span class="type">uint32_t</span> stage = <span class="number">0</span>; stage &lt; stages; ++stage) &#123;</span><br><span class="line">    <span class="type">const</span> std::vector&lt;sftensor&gt; stage_output = stage_outputs.<span class="built_in">at</span>(stage);</span><br><span class="line">    <span class="type">const</span> <span class="type">uint32_t</span> nx_ = stage_output.<span class="built_in">front</span>()-&gt;<span class="built_in">rows</span>();</span><br><span class="line">    <span class="type">const</span> <span class="type">uint32_t</span> ny_ = stage_output.<span class="built_in">front</span>()-&gt;<span class="built_in">cols</span>();</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">uint32_t</span> i = <span class="number">0</span>; i &lt; stage_output.<span class="built_in">size</span>(); ++i) &#123;</span><br><span class="line">      <span class="built_in">CHECK</span>(stage_output.<span class="built_in">at</span>(i)-&gt;<span class="built_in">rows</span>() == nx_ &amp;&amp;</span><br><span class="line">          stage_output.<span class="built_in">at</span>(i)-&gt;<span class="built_in">cols</span>() == ny_);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt; x_stages_tensor;</span><br><span class="line">    x_stages_tensor =</span><br><span class="line">        <span class="built_in">TensorCreate</span>(batch_size, stages * nx_ * ny_, <span class="built_in">uint32_t</span>(classes_info));</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> omp parallel for num_threads(batch_size)</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">uint32_t</span> b = <span class="number">0</span>; b &lt; batch_size; ++b) &#123;</span><br><span class="line">      <span class="comment">// 遍历每一批次</span></span><br><span class="line">      <span class="type">const</span> std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt; &amp;input = stage_output.<span class="built_in">at</span>(b);</span><br><span class="line">      <span class="built_in">CHECK</span>(input != <span class="literal">nullptr</span> &amp;&amp; !input-&gt;<span class="built_in">empty</span>());</span><br><span class="line">      <span class="type">const</span> <span class="type">uint32_t</span> nx = input-&gt;<span class="built_in">rows</span>();</span><br><span class="line">      <span class="type">const</span> <span class="type">uint32_t</span> ny = input-&gt;<span class="built_in">cols</span>();</span><br><span class="line">      <span class="comment">// 将input张量reshape到对应的形状(stages, classes_info, ny*nx)</span></span><br><span class="line">      input-&gt;<span class="built_in">ReRawView</span>(&#123;stages, <span class="built_in">uint32_t</span>(classes_info), ny * nx&#125;);</span><br><span class="line">      <span class="type">const</span> <span class="type">uint32_t</span> size = input-&gt;<span class="built_in">size</span>();</span><br><span class="line"></span><br><span class="line">      <span class="comment">// x[i].sigmoid()</span></span><br><span class="line">      input-&gt;<span class="built_in">Transform</span>(</span><br><span class="line">          [](<span class="type">const</span> <span class="type">float</span> value) &#123; <span class="keyword">return</span> <span class="number">1.f</span> / (<span class="number">1.f</span> + <span class="built_in">expf</span>(-value)); &#125;);</span><br><span class="line"></span><br><span class="line">      <span class="comment">// .split(2, 2, self.nc + 1), 4)</span></span><br><span class="line">      arma::fmat &amp;x_stages = x_stages_tensor-&gt;<span class="built_in">slice</span>(b);</span><br><span class="line">      <span class="keyword">for</span> (<span class="type">uint32_t</span> s = <span class="number">0</span>; s &lt; stages; ++s) &#123;</span><br><span class="line">        x_stages.<span class="built_in">submat</span>(ny * nx * s, <span class="number">0</span>, ny * nx * (s + <span class="number">1</span>) - <span class="number">1</span>,</span><br><span class="line">                        classes_info - <span class="number">1</span>) = input-&gt;<span class="built_in">slice</span>(s).<span class="built_in">t</span>();</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      <span class="comment">// xy = (xy * 2 + self.grid[i]) * self.stride[i]</span></span><br><span class="line">      <span class="type">const</span> arma::fmat &amp;xy = x_stages.<span class="built_in">submat</span>(<span class="number">0</span>, <span class="number">0</span>, x_stages.n_rows - <span class="number">1</span>, <span class="number">1</span>);</span><br><span class="line">      <span class="comment">// wh = (wh * 2) ** 2 * self.anchor_grid[i]</span></span><br><span class="line">      <span class="type">const</span> arma::fmat &amp;wh = x_stages.<span class="built_in">submat</span>(<span class="number">0</span>, <span class="number">2</span>, x_stages.n_rows - <span class="number">1</span>, <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">     <span class="comment">// y = torch.cat((xy, wh, conf), 4)</span></span><br><span class="line">      x_stages.<span class="built_in">submat</span>(<span class="number">0</span>, <span class="number">0</span>, x_stages.n_rows - <span class="number">1</span>, <span class="number">1</span>) =</span><br><span class="line">          (xy * <span class="number">2</span> + grids_[stage]) * strides_[stage];</span><br><span class="line">      x_stages.<span class="built_in">submat</span>(<span class="number">0</span>, <span class="number">2</span>, x_stages.n_rows - <span class="number">1</span>, <span class="number">3</span>) =</span><br><span class="line">          arma::<span class="built_in">pow</span>((wh * <span class="number">2</span>), <span class="number">2</span>) % anchor_grids_[stage];</span><br><span class="line">    &#125;</span><br><span class="line">    concat_rows += x_stages_tensor-&gt;<span class="built_in">rows</span>();</span><br><span class="line">    <span class="comment">// 一个stage（检测头）中所有批次的数据在处理完之后都会被放到x_stages和zs.at(stage)的位置</span></span><br><span class="line">    zs.<span class="built_in">at</span>(stage) = x_stages_tensor;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 将三个检测头的输出重新拼接起来，并存放到f1的位置</span></span><br><span class="line">  <span class="type">uint32_t</span> current_rows = <span class="number">0</span>;</span><br><span class="line">  <span class="function">arma::fcube <span class="title">f1</span><span class="params">(concat_rows, classes_info, batch_size)</span></span>;</span><br><span class="line">  <span class="keyword">for</span> (<span class="type">const</span> <span class="keyword">auto</span> &amp;z : zs) &#123;</span><br><span class="line">    f1.<span class="built_in">subcube</span>(current_rows, <span class="number">0</span>, <span class="number">0</span>, current_rows + z-&gt;<span class="built_in">rows</span>() - <span class="number">1</span>,</span><br><span class="line">               classes_info - <span class="number">1</span>, batch_size - <span class="number">1</span>) = z-&gt;<span class="built_in">data</span>();</span><br><span class="line">    current_rows += z-&gt;<span class="built_in">rows</span>();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; f1.n_slices; ++i) &#123;</span><br><span class="line">    std::shared_ptr&lt;Tensor&lt;<span class="type">float</span>&gt;&gt; output = outputs.<span class="built_in">at</span>(i);</span><br><span class="line">    <span class="keyword">if</span> (output == <span class="literal">nullptr</span> || output-&gt;<span class="built_in">empty</span>()) &#123;</span><br><span class="line">      output = std::make_shared&lt;Tensor&lt;<span class="type">float</span>&gt;&gt;(<span class="number">1</span>, concat_rows, classes_info);</span><br><span class="line">      outputs.<span class="built_in">at</span>(i) = output;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">CHECK</span>(output-&gt;<span class="built_in">rows</span>() == f1.<span class="built_in">slice</span>(i).n_rows);</span><br><span class="line">    <span class="built_in">CHECK</span>(output-&gt;<span class="built_in">cols</span>() == f1.<span class="built_in">slice</span>(i).n_cols);</span><br><span class="line">    output-&gt;<span class="built_in">slice</span>(<span class="number">0</span>) = std::<span class="built_in">move</span>(f1.<span class="built_in">slice</span>(i));</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> InferStatus::kInferSuccess;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://kilogrand.gitee.io">kiloGrand</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://kilogrand.gitee.io/2023/03/24/kuiper_infer-L14/">https://kilogrand.gitee.io/2023/03/24/kuiper_infer-L14/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://kilogrand.gitee.io" target="_blank">kiloGrand</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/kuiper-infer/">kuiper_infer</a></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-full"><a href="/2023/03/23/kuiper_infer-L13/"><img class="prev-cover" src="/img/coding.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">自制深度学习框架--实现ResNet网络的推理</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2023/03/20/kuiper_infer-L10/" title="自制深度学习框架--Im2Col原理与卷积层的实现"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-20</div><div class="title">自制深度学习框架--Im2Col原理与卷积层的实现</div></div></a></div><div><a href="/2023/03/21/kuiper_infer-L11/" title="自制深度学习框架--再探Tensor类并构建计算图的图关系"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-21</div><div class="title">自制深度学习框架--再探Tensor类并构建计算图的图关系</div></div></a></div><div><a href="/2023/03/22/kuiper_infer-L12/" title="自制深度学习框架--算子的执行流程"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-22</div><div class="title">自制深度学习框架--算子的执行流程</div></div></a></div><div><a href="/2023/03/23/kuiper_infer-L13/" title="自制深度学习框架--实现ResNet网络的推理"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-23</div><div class="title">自制深度学习框架--实现ResNet网络的推理</div></div></a></div><div><a href="/2023/03/14/kuiper_infer-L3/" title="自制深度学习框架--导入数据"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-14</div><div class="title">自制深度学习框架--导入数据</div></div></a></div><div><a href="/2023/03/13/kuiper_infer-L2/" title="自制深度学习框架--张量"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-13</div><div class="title">自制深度学习框架--张量</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/profile.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">kiloGrand</div><div class="author-info__description">coder && data-science researcher</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">46</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/kiloGrand/"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#SiLU"><span class="toc-number">1.</span> <span class="toc-text">SiLU</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Concat"><span class="toc-number">2.</span> <span class="toc-text">Concat</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#UpSample"><span class="toc-number">3.</span> <span class="toc-text">UpSample</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#YoloDetect"><span class="toc-number">4.</span> <span class="toc-text">YoloDetect</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/24/kuiper_infer-L14/" title="自制深度学习框架--实现Yolov5的推理">自制深度学习框架--实现Yolov5的推理</a><time datetime="2023-03-24T12:00:00.000Z" title="发表于 2023-03-24 20:00:00">2023-03-24</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/23/kuiper_infer-L13/" title="自制深度学习框架--实现ResNet网络的推理">自制深度学习框架--实现ResNet网络的推理</a><time datetime="2023-03-23T12:00:00.000Z" title="发表于 2023-03-23 20:00:00">2023-03-23</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/22/kuiper_infer-L12/" title="自制深度学习框架--算子的执行流程">自制深度学习框架--算子的执行流程</a><time datetime="2023-03-22T12:00:00.000Z" title="发表于 2023-03-22 20:00:00">2023-03-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/21/kuiper_infer-L11/" title="自制深度学习框架--再探Tensor类并构建计算图的图关系">自制深度学习框架--再探Tensor类并构建计算图的图关系</a><time datetime="2023-03-21T12:00:00.000Z" title="发表于 2023-03-21 20:00:00">2023-03-21</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/20/kuiper_infer-L10/" title="自制深度学习框架--Im2Col原理与卷积层的实现">自制深度学习框架--Im2Col原理与卷积层的实现</a><time datetime="2023-03-20T12:00:00.000Z" title="发表于 2023-03-20 20:00:00">2023-03-20</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2023 By kiloGrand</div><div class="footer_custom_text">Hi, welcome to my blog!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.2
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container:not\([display]\)').forEach(node => {
            const target = node.parentNode
            if (target.nodeName.toLowerCase() === 'li') {
              target.parentNode.classList.add('has-jax')
            } else {
              target.classList.add('has-jax')
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>